package com.opennetexam.manager.itemmodel;

import com.openexam.openqti.openqtipaperquestion.OpenItemResult;
import com.openexam.openqti.openqtipaperquestion.OpenResultResponseVariable;
import com.openexam.openqti.openqtipaperquestion.OpenScoreOutcomeVariable;
import com.openexam.openqti.qti.BaseTypeType;
import com.openexam.openqti.qti.CardinalityType;
import com.openexam.openqti.qti.SessionStatusType;
import com.opennetexam.manager.common.CommonFunction;
import com.opennetexam.manager.dto.AnswerModelWithCorr;
import com.opennetexam.manager.dto.ItemTemplate;
import com.opennetexam.manager.utils.DateUtil;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 奶茶
 * Created by on 2017/12/14.
 * 阅读理解
 */
public class ItemReadingComprehensionCreater extends IHtmlCreater{

    @Override
    public OpenItemResult save(ItemTemplate template) {
        //考生答案
        List<String> candidateResponses = template.getCandidateResponses();
        //正确答案
        List<String> correctResponses = template.getCorrectResponses();
        double scores = 0;

        List<OpenResultResponseVariable> responseList = new ArrayList<>();
        List<OpenScoreOutcomeVariable> outcomeList = new ArrayList<>();

        Integer index = 1;
        for (ItemTemplate itemTemplate:template.getSubItems()) {
            Double score = CommonFunction.getDoubleDiv(template.getScore(), Double.valueOf(String.valueOf(template.getSubItems().size())));
            OpenResultResponseVariable openResultResponseVariable = new OpenResultResponseVariable(RESPONSE_ + index, CardinalityType.SINGLE, BaseTypeType.IDENTIFIER, correctResponses.get(index - 1), candidateResponses.get(index - 1));
            responseList.add(openResultResponseVariable);

            //小题判分
            if(correctResponses.get(index-1).equals(candidateResponses.get(index-1))){
                OpenScoreOutcomeVariable openScoreOutcomeVariable = new OpenScoreOutcomeVariable("SCORE_" + index, score, score);
                outcomeList.add(openScoreOutcomeVariable);
                scores += score;
            }else{
                OpenScoreOutcomeVariable openScoreOutcomeVariable = new OpenScoreOutcomeVariable("SCORE_" + index, score, 0);
                outcomeList.add(openScoreOutcomeVariable);
                scores += 0;
            }
            index++;
        }
        OpenScoreOutcomeVariable scoreVariable = new OpenScoreOutcomeVariable("SCORE", template.getScore(), scores);
        outcomeList.add(scoreVariable);
        return new OpenItemResult(template.getIdentifier(), DateUtil.dateToXmlDate(new Date()),template.getSequenceIndex(), SessionStatusType.INITIAL,0,template.getMarked(),CommonFunction.getStatus(template.getItemCompletionStatus()),responseList,outcomeList);
    }

    @Override
    public String getPreview(Boolean isShowAnswer) {
        throw new NotImplementedException();
    }

    @Override
    public List<String> getItemResult(OpenItemResult openItemResult) {
        throw new NotImplementedException();
    }

    @Override
    public double judeScore(AnswerModelWithCorr answerModel) {
        throw new NotImplementedException();
    }
}
